package lab_2_server;

import java.net.Socket;

/**
 * Identificator other servers
 * @author pacific
 */
public class ServerID {

    public ServerID(String host, int port, String alias) {
        setHost(host);
        setPort(port);
        setAlias( alias );
    }


    /** Try connect ot this ServerID */
    public Socket connect() {
        Socket socket = null ;
        try {
            socket = new Socket(getHost(), getPort());
        } catch ( Exception ex ) {
            MyLogger.putMessange( "Not cane get connect with remote address. " + ex) ;
        }
        if( socket != null )
            setConnected( true );
        return socket ;
    }


    @Override
    public String toString() {
        return getAlias()+"/"+getHost()+":"+getPort() ;
    }
    @Override
    public boolean equals(Object obj) {
        if( obj == this )
            return true ;
        if( obj instanceof ServerID ) {
            ServerID id = (ServerID) obj ;
            if(     getHost() == id.getHost() &&
                    getPort() == id.getPort() )
                return true ;
        }
        return false ;
    }
    @Override
    public int hashCode() {
        return getHost().hashCode() + getPort() ;
    }


    //-----Fields-----

    private boolean isConnected = false ;
    private int port;
    private String host;
    private String alias ;


    //-----Getters and Setters-----

    //For "port"
    public int getPort() {
        return port;
    }
    private void setPort(int port) {
        this.port = port;
    }
    //For "host"
    public String getHost() {
        return host;
    }
    private void setHost(String host) {
        this.host = host;
    }
    //For "alias"
    public String getAlias() {
        return alias;
    }
    private void setAlias(String alias) {
        this.alias = alias;
    }
    //For "isConnected"
    public boolean isConnected() {
        return isConnected;
    }
    public void setConnected(boolean isConnected) {
        this.isConnected = isConnected;
    }
}